// 创建一个单独管理首页数据的 store
import { defineStore } from 'pinia'
// 导入类型
 import { ApiRes, BannerItem, newItem, GoodsItem, HotItem, BrandItem, ProductItem, SpecialItem } from '@/types/data'
// 导入 axios
import request from '@/utils/request'
const useHomeStore = defineStore('home', {
  state: () => {
    return {
      // 定义轮播图的数据
      bannerList: [] as BannerItem[],
      // 新鲜好物的数据源
      newList: [] as GoodsItem[],
      // 人气推荐数据源
      hotList: [] as HotItem[],
      //热门品牌数据
      brandList: [] as BrandItem[],
      //产品的数据
      productList:[] as ProductItem[],
      // 最新专题数据
      specialList: [] as SpecialItem[]
    }
  },
  actions: {
    // 得到轮播图数据
    async getBannerList () {
      const res = await request.get<ApiRes<BannerItem[]>>('/home/banner')
      this.bannerList = res.data.result
    },
    // 得到新鲜好物的数据源
    async getNewList () {
      const res = await request.get<ApiRes<newItem>>('/home/new/simple')
      setTimeout(() => {
        this.newList = res.data.result.newProduct
      }, 5000);
    },
    // 得到人气推荐数据源
    async getHotList () {
      const res = await request.get<ApiRes<HotItem[]>>('/home/hot')
      setTimeout(() => {
        this.hotList = res.data.result
      }, 5000);
    },
    //得到热门品牌的数据
    async getBrandList() {
      const res = await request.get<ApiRes<BrandItem[]>>('/home/brand')
      setTimeout(() => {
        this.brandList = res.data.result
      }, 2000);
    },
    //得到产品取土的数据
    async getProductList(){
      const res = await request.get<ApiRes<ProductItem[]>>('/home/goods')
      this.productList = res.data.result
    },
    // 得到最新专题的数据
    async getSpecialList () {
      const res = await request.get<ApiRes<SpecialItem[]>>('/home/special')
      this.specialList = res.data.result
    }
  },
  getters: {}
})

export default useHomeStore
